//
//  XZHHomeNoteCollectionViewController.m
//  Piece
//
//  Created by dllo on 16/6/21.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHHomeNoteCollectionViewController.h"
#import "XZHHomeListCollectionViewCell.h"
#import "XZHHomeNoteModel.h"
#import "DYJDataBaseTool.h"
#import "XZHHomeNoteViewController.h"
#import "XZHHomeNoteDetailViewController.h"
#import "UIImageView+WebCache.h"

@interface XZHHomeNoteCollectionViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UIImageView *noCollectionImageView;
@end

@implementation XZHHomeNoteCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.86 green:0.87 blue:0.88 alpha:1.00];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self createTitle:@"我的小记"];
    [super viewDidLoad];
    [self setUpCollectionView];
    [self getDataFromDataBase];
    // Do any additional setup after loading the view.
}

- (void)setUpCollectionView {
    
    //创建系统提供的布局对象
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    //设置每个单元格的尺寸
    flowLayOut.itemSize = CGSizeMake(ScreenWidth / 2 - 7, ScreenHeight / 5);
    //最小列间距
    flowLayOut.minimumInteritemSpacing = 0;
    //最小行间距
    flowLayOut.minimumLineSpacing = 5;
    //设置内容区域与周围的距离
    flowLayOut.sectionInset = UIEdgeInsetsMake(5, 0, 5, 5);
    //设置滚动方向
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    //初始化collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 0, ScreenWidth - 5, ScreenHeight - 64 - 49) collectionViewLayout:flowLayOut];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    
    _noCollectionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 20, 360, 240)];
    _noCollectionImageView.image = [UIImage imageNamed:@"collection"];
    [_collectionView addSubview:_noCollectionImageView];
    
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"XZHHomeListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"listCell"];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XZHHomeListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"listCell" forIndexPath:indexPath];
    XZHHomeNoteModel *model = [self.dataSource objectAtIndex:indexPath.row];
    if ([model.image isEqualToString:@"(null)"]) {
        [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.placeholderImage]];
    } else {
        NSData *_decodedImageData = [[NSData alloc] initWithBase64Encoding:model.image];
        UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
        cell.coverImageView.image = _decodedImage;
    }
    
//    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.hp_img_url]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    XZHHomeNoteDetailViewController *ndvc = [[XZHHomeNoteDetailViewController alloc] init];
    ndvc.model = [self.dataSource objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:ndvc animated:YES];
}

- (void)getDataFromDataBase
{
    DYJDataBaseTool *dataBaseTool = [DYJDataBaseTool shareDYJDataBaseTool];
    NSMutableArray *array = [dataBaseTool selectAllNote];
    self.dataSource = [NSMutableArray arrayWithArray:array];
    
    if (_dataSource.count == 0) {
        _noCollectionImageView.hidden = NO;
    }
    else
    {
        _noCollectionImageView.hidden = YES;
    }
    [_collectionView reloadData];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
